package com.example.leon.demo.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.leon.demo.adbanner.VisaImageCycleView;
import com.example.leon.demo.common.BaseActivity;
import com.example.leon.demo.R;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;
import com.example.leon.demo.viewmodel.AdInfo;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.example.leon.demo.Base.ImageLoaderHelper;
import com.example.leon.demo.utils.CommonUtil;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

/**
 * Created by wang.long on 2016/4/12.
 */
public class CycleViewPager extends BaseActivity {
    VisaImageCycleView imageCycleView;
    @Override
    protected void onCreate(Bundle saveInstanceState){
        super.onCreate(saveInstanceState);

        super.setContentView(R.layout.cycleviewpager);
        //初始化首页广告
        initAD();
    }


    //初始化首页广告
    public void initAD() {

        // 初始化ImageLoader
        @SuppressWarnings("deprecation")
        DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.call_5) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.call_6) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.call_7) // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
                .build(); // 创建配置过得DisplayImageOption对象

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).defaultDisplayImageOptions(options)
                .threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO).build();
        ImageLoader.getInstance().init(config);


        imageCycleView = (VisaImageCycleView) findViewById(R.id.indexCycleView);
        AdInfo adInfo = new AdInfo();
        ArrayList<String> imageUrlList = new ArrayList<String>();
        ArrayList<String> clickUrlList = new ArrayList<String>();
        ArrayList<String> advertisementPosition = new ArrayList<String>();
        //简析报文
        for (int i = 0; i < 3; i++) {
            switch (i){
                case 0:
                    imageUrlList.add("http://images3.c-ctrip.com/dj/gt/20160226/yinghua750_360.jpg");
                    clickUrlList.add("photo"+i);
                    advertisementPosition.add(String.valueOf(i));
                    break;
                case 1:
                    imageUrlList.add("http://images3.c-ctrip.com/dj/app/201603/online_visa750_360.jpg");
                    clickUrlList.add("photo"+i);
                    advertisementPosition.add(String.valueOf(i));
                    break;
                case 2:
                    imageUrlList.add("http://images3.c-ctrip.com/dj/app/201603/taiwan750_360.jpg");
                    clickUrlList.add("photo"+i);
                    advertisementPosition.add(String.valueOf(i));
                    break;
            }
        }
        adInfo.ClickUrlList = clickUrlList;
        adInfo.ImageUrlList = imageUrlList;
        adInfo.AdvertisementPositionList = advertisementPosition;

        initCarsuelView(adInfo.ClickUrlList, adInfo.ImageUrlList);


    }

    //初始化轮播图
    public void initCarsuelView(final ArrayList<String> imageDescList, ArrayList<String> urlList) {

        LinearLayout.LayoutParams cParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, CommonUtil.getScreenWidth(CycleViewPager.this) * 35 / 65);
        imageCycleView.setLayoutParams(cParams);
        VisaImageCycleView.ImageCycleViewListener mAdCycleViewListener = new VisaImageCycleView.ImageCycleViewListener() {
            @Override
            public void onImageClick(int position, View imageView) {
                /**实现点击事件*/
                String url = imageDescList.get(position);

                Toast.makeText(CycleViewPager.this,url,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void displayImage(String imageURL, ImageView imageView) {
                ImageLoader.getInstance().displayImage(imageURL,imageView);
            }
        };
        /**设置数据*/
        imageCycleView.setImageResources(imageDescList, urlList, mAdCycleViewListener);
        imageCycleView.startImageCycle();
    }
}
